package com.hmdp.service.impl;

import cn.hutool.json.JSONUtil;
import com.hmdp.dto.Result;
import com.hmdp.entity.Shop;
import com.hmdp.entity.ShopType;
import com.hmdp.mapper.ShopTypeMapper;
import com.hmdp.service.IShopTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmdp.utils.RedisConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 虎哥
 * @since 2021-12-22
 */
@Service
public class ShopTypeServiceImpl extends ServiceImpl<ShopTypeMapper, ShopType> implements IShopTypeService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public List<ShopType> queryTypeList() {
        //0.1先查出共有多少个店铺类型
        long count = count();
        //1.先去redis中查
        List<ShopType> list = redisTemplate.opsForList().range(RedisConstants.SHOP_TYPE, 0, count);
        //2.判断是否为空
        if (list != null && !list.isEmpty()) {
            //3.不为空直接返回list
            return list;
        }
        //4.为空去数据库查询
        List<ShopType> typeList = list();
        //5.判断是否为空
        if(typeList == null){
            return null;
        }
        //6.不为空保存到redis
        redisTemplate.opsForList().leftPushAll(RedisConstants.SHOP_TYPE,typeList);
        //7.返回
        return typeList;
    }
}
